﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public class KyThiProvider
    {
        public static List<KyThi> LayDanhSachKyThi()
        {
            var lst = new List<KyThi>();
            using (var context = new QLTTTHEntities())
            {
                var query = (from u in context.KyThis select u);

                foreach (var row in query)
                {
                    var kt = new KyThi();
                    kt.MaKyThi = row.MaKyThi;
                    kt.MaKH = row.MaKH;
                    kt.NgayThi = row.NgayThi;
                    kt.HinhThucThi = row.HinhThucThi;
                    kt.ThoiGianThi = row.ThoiGianThi;

                    lst.Add(kt);
                }
            }

            return lst;
        }

        public static int ThemKyThi(KyThi kt)
        {
            int maKT = -1;
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    context.KyThis.Add(kt);
                    context.SaveChanges();
                    maKT = kt.MaKyThi;
                }
            }
            catch
            {

            }
            return maKT;
        }

        public static bool XoaKyThi(int maKT)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    var query = (from u in context.KyThis
                                 where u.MaKyThi.Equals(maKT)
                                 select u).First();

                    context.KyThis.Remove(query);
                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool CapNhatKyThi(KyThi kt)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    var query = (from u in context.KyThis
                                 where u.MaKyThi.Equals(kt.MaKyThi)
                                 select u).First();
                    query.MaKH = kt.MaKH;
                    query.NgayThi = kt.NgayThi;
                    query.HinhThucThi = kt.HinhThucThi;
                    query.ThoiGianThi = kt.ThoiGianThi;

                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
